home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 32-bit_x86 / Francais / cpsimage.cab / data / sys / vadof.elf < prev    next >
Text File  |  2009-03-16  |  21KB  |  582 lines

  1. /* $Id: vadof.elf,v 1.15 2008/10/27 01:53:00 mfrey Exp $ */
  2.  
  3. /* vadof - Validate a Directory of Files
  4. ** This file contains a script used to validate a group of files in
  5. ** a given directory.
  6. */
  7. #load "sys/stdio.elf";
  8. #load "sys/datetime.elf";
  9. #load "sys/xipxml.elf";
  10. #load "sys/argTokenClass.elf";
  11. /*****************************************************************************/
  12. /* @VADOF */
  13. /* @New VADOF - Validate a directory of files - This class is used to control
  14. a workflow of file validation. The workflow will compare validation data
  15. generated for files in a given directory against a master list of validations.
  16. Construct a new instance with the name of the directory that has the files and
  17. the name of the file that has the master list.
  18. The results are:
  19.    A count of number fo files found not on the list and number of errors
  20.    A list of files found that are not one the list
  21.    A list of errors found
  22.    A new master list that would result in no errors.
  23. */
  24. /* @diff  Get difference data where success will be FALSE if there is a
  25. processing error, errCnt will give the number of validation mis-matches,
  26. and newCnt will provide the number of files in the directory that are not
  27. in the current master list */
  28. /* @logErrors Write the list of errors and new files to a text file. Will
  29. return FALSE for any failure mode. */
  30. /* @updateValidateFile  Given the name of a file, a new XML file will be written
  31. that contains the actual validation data for the given directory. */
  32. /* @updateValidateXML Get the actual validation data for all of the files in
  33. in the given dicrectory. The data is retuyrned in an XML DOM. */
  34. /*****************************************************************************/
  35.  
  36. CLASS VADOF
  37. {
  38.     METHOD New(STRING directory, STRING validation, LIST filters) {
  39.         this.fileDirectory = directory;
  40.         this.validFile = validation;
  41.         this.isProcessed = FALSE;
  42.         this.isValid = FALSE;
  43.     this.fileFilters = filters; 
  44.     }
  45.  
  46.     METHOD diff ( )
  47.         RETURNS (BOOLEAN success, INTEGER errCnt, INTEGER newCnt)
  48.     {
  49.         this.runProcess();
  50.         success = this.isValid;
  51.         if (success == TRUE) {
  52.             errCnt = this.invalExistingFiles.length()+this.missingFiles.length();
  53.             newCnt = this.filesWithoutVal.length();
  54.         } else {
  55.             errCnt = 0;
  56.             newCnt = 0; 
  57.         }
  58.     }
  59.  
  60.     METHOD logErrors(STRING filename) RETURNS (BOOLEAN success)
  61.     {
  62.         this.runProcess();
  63.         success = this.writeLog(filename: filename);
  64.     }
  65.  
  66.     METHOD updateValidateFile(STRING filename) RETURNS (BOOLEAN success)
  67.     {
  68.         XmlDocument vaData = this.getNewValidate();
  69.         if (this.isValid == TRUE) {
  70.             vaData.dumpFile(filename: filename, format: 1);
  71.             success = TRUE;
  72.         } else {
  73.             success = FALSE;
  74.         }
  75.     }
  76.  
  77.     METHOD updateValidateXML( ) RETURNS (XmlDocument vaData)
  78.     {
  79.         vaData = this.getNewValidate();
  80.     }
  81.  
  82.     /*****************************************************************************
  83.     ********************    Lots of Private Methods ******************************
  84.     *****************************************************************************/
  85.  
  86.     private METHOD runProcess( )
  87.     {
  88.         if (this.isProcessed == TRUE) return;
  89.         this.isProcessed  = TRUE;
  90.         this.prepareNodes();
  91.         if (this.isValid == FALSE) return;
  92.         this.prepareFiles();
  93.         if (this.isValid == FALSE) return;
  94.         this.runValidate();
  95.     }
  96.  
  97.     private METHOD prepareNodes()
  98.     {
  99.         /* Test for filename (no name is okay), then make sure the
  100.         ** given name is really a XML with correct root. Given a correct
  101.         ** DOM, pass it to the parser */
  102.         if (this.validFile) {
  103.             XmlDocumentBuilder db;
  104.             XmlDocument vdoc = db.parseFile(filename: this.validFile);
  105.             if (!vdoc) {
  106.                 this.isValid = FALSE;
  107.                 return;
  108.             }
  109.             XmlNode root = vdoc.getDocumentElement();
  110.             STRING docname = root.getNodeName();
  111.             if (docname == "xipsValidation") {
  112.                 this.ingestDOM(vdoc: vdoc);
  113.             } else {
  114.                 this.isValid = FALSE;
  115.                 return;
  116.             }
  117.         }
  118.         this.isValid = TRUE;
  119.     }
  120.  
  121.     private METHOD prepareFiles()
  122.     {
  123.         FILE dirlst = new(FILE, path: this.fileDirectory);
  124.         if (!dirlst) {
  125.             this.isValid = FALSE;
  126.             return;
  127.         }
  128.         if (dirlst.isDirectory() == TRUE) {
  129.             this.fileList = dirlst.listFiles();
  130.         if(this.fileFilters){
  131.            this.trimList(); 
  132.         }
  133.         this.fileList.sort(lexical: 1, byname: 1);
  134.         }
  135.         this.isValid = TRUE;
  136.     }
  137.  
  138.     private METHOD trimList()
  139.     {
  140.         
  141.     LIST ltemp;
  142.     FILE ftemp; 
  143.     INTEGER len = this.fileList.length(); 
  144.     INTEGER i ;
  145.     STRING stemp ;  
  146.     for(i=0;i<len;i++){
  147.           ftemp = this.fileList.ref(entry: i); 
  148.       stemp = ftemp.getExt(); 
  149.           if( this.fileFilters.search(name: stemp) != -1){
  150.         ltemp.insert(obj: ftemp, name: ftemp.getName());     
  151.       } 
  152.     }
  153.     this.fileList = ltemp;  
  154.     }
  155.  
  156.     private METHOD runValidate()
  157.     {
  158.         /* STEP 1: use the list of files to search the list of validation
  159.         ** elements for matching names, test validation value, record results
  160.         ** and remove the validation element */
  161.         if ((this.fileList) && (this.fileList.length() > 0)) {
  162.             if ((this.nodeList) && (this.nodeList.length() > 0)) {
  163.                 this.fileProcWithList();
  164.             } else {
  165.                 this.fileProcNoList();
  166.             }
  167.         }
  168.  
  169.         /* STEP 2: If any validation elements remain, record them as errors */
  170.         if ((this.nodeList) && (this.nodeList.length() > 0)) {
  171.             INTEGER len = this.nodeList.length();
  172.             INTEGER i;
  173.             STRING fiName;
  174.             for (i=0; i<len; i++) {
  175.                 fiName = this.nodeList.name(entry: i);
  176.                 this.missingFiles.insert(name: fiName, obj: fiName);
  177.             }
  178.             this.nodeList.clear();
  179.         }
  180.     }
  181.  
  182.     private METHOD fileProcNoList ()
  183.     {
  184.         FILE curFi;
  185.         INTEGER fiCnt = this.fileList.length();
  186.         INTEGER i;
  187.         STRING curName;
  188.         /* Just add them all to the "no data" list */
  189.         for (i=0; i<fiCnt; i++) {
  190.             curFi = this.fileList.ref(entry: i);
  191.             if (curFi.isFile() == TRUE) {
  192.                 curName = curFi.getName();
  193.                 this.filesWithoutVal.insert(name: curName,
  194.                                          obj: this.calcSum(src: curFi));
  195.             }
  196.         }
  197.     }
  198.  
  199.     private METHOD fileProcWithList()
  200.     {
  201.         FILE curFi;
  202.         INTEGER fiCnt = this.fileList.length();
  203.         INTEGER i;
  204.         INTEGER ind;
  205.         STRING curName;
  206.         STRING oldMD;
  207.         STRING newMD;
  208.         for (i=0; i<fiCnt; i++) {
  209.             curFi = this.fileList.ref(entry: i);
  210.             if (curFi.isFile() == TRUE) {
  211.                 curName = curFi.getName();
  212.                 ind = this.nodeList.search(name: curName);
  213.                 if (ind >= 0) {
  214.                     oldMD = this.nodeList.extract(entry: ind);
  215.                     newMD = this.calcSum(src: curFi);
  216.                     if (oldMD == newMD) {
  217.                         this.validExistingFiles.insert(name: curName, obj: oldMD);
  218.                     } else {
  219.                         this.invalExistingFiles.insert(name: curName, obj: newMD);
  220.                     }
  221.                 } else {
  222.                     this.filesWithoutVal.insert(name: curName,
  223.                                              obj: this.calcSum(src: curFi));
  224.                 }    /* END else block of if ind >= 0 */
  225.             }    /* END if isFile */
  226.         }    /* END for fiCnt */
  227.     }
  228.  
  229.     private METHOD calcSum (FILE src) RETURNS (STRING mdHash)
  230.     {
  231.         mdHash = MD5(filename: src.getCanonicalPath());
  232.     }
  233.  
  234.  
  235.     /****************************************************************************/
  236.     /************ private methods that:   Output errors *************************/
  237.     /****************************************************************************/
  238.     private METHOD writeLog(STRING filename) RETURNS (BOOLEAN success)
  239.     {
  240.         /* TODO Add linefeeds & CRs to each write */
  241.         STRING lineBuf;
  242.         this.runProcess();
  243.         success = TRUE;
  244.         this.writeHeader(filename: filename);
  245.         if (this.isValid == FALSE) {
  246.             lineBuf = "Failed to process validation correctly\n";
  247.             lineBuf.Write(filename: filename, value: 1, mode: "a");
  248.             return;
  249.         }
  250.         this.writeError(filename: filename);
  251.         this.writeAbsent(filename: filename);
  252.         this.writeNewFiles(filename: filename);
  253.     }
  254.  
  255.     private METHOD writeHeader(STRING filename)
  256.     {
  257.         DATETIME tm;
  258.         tm.setToCurrent(utc: FALSE);
  259.         STRING lineBuf = "This file generated by VADOF on " +
  260.                           tm.toString()+"\n\n";
  261.         lineBuf.Write(filename: filename, value: 1);
  262.     }
  263.  
  264.     private METHOD writeError(STRING filename)
  265.     {
  266.         STRING lineBuf;
  267.         INTEGER len;
  268.  
  269.         if ((this.invalExistingFiles) && (this.invalExistingFiles.length() > 0)) {
  270.             len = this.invalExistingFiles.length();
  271.             INTEGER i;
  272.             lineBuf = "The following files had checksum errors \n\n";
  273.             lineBuf.Write(filename: filename, value: 1, mode: "a");
  274.             lineBuf.Write(value: 1);
  275.             for (i=0; i<len; i++) {
  276.                 lineBuf = "    "+this.invalExistingFiles.name(entry:i)+"\n";
  277.                 lineBuf.Write(filename: filename, value: 1, mode: "a");
  278.                 lineBuf.Write(value: 1);
  279.             }
  280.         } else {
  281.             lineBuf = "No checksum errors for existing files found\n";
  282.             lineBuf.Write(filename: filename, value: 1, mode: "a");
  283.             lineBuf.Write(value: 1);
  284.         }
  285.         lineBuf = "\n\n";
  286.         lineBuf.Write(filename: filename, value: 1, mode: "a");
  287.         lineBuf.Write(value: 1);
  288.     }
  289.  
  290.     private METHOD writeAbsent(STRING filename)
  291.     {
  292.         STRING lineBuf;
  293.         INTEGER len;
  294.  
  295.         if ((this.missingFiles) && (this.missingFiles.length() > 0)) {
  296.             len = this.missingFiles.length();
  297.             INTEGER i;
  298.             lineBuf = "The following files were missing \n\n";
  299.             lineBuf.Write(filename: filename, value: 1, mode: "a");
  300.             lineBuf.Write(value: 1);
  301.             for (i=0; i<len; i++) {
  302.                 lineBuf = "    "+this.missingFiles.name(entry:i)+"\n";
  303.                 lineBuf.Write(filename: filename, value: 1, mode: "a");
  304.                 lineBuf.Write(value: 1);
  305.             }
  306.         } else {
  307.             lineBuf = "No files were missing\n";
  308.             lineBuf.Write(filename: filename, value: 1, mode: "a");
  309.             lineBuf.Write(value: 1);
  310.         }
  311.         lineBuf = "\n\n";
  312.         lineBuf.Write(filename: filename, value: 1, mode: "a");
  313.         lineBuf.Write(value: 1);
  314.     }
  315.  
  316.     private METHOD writeNewFiles(STRING filename)
  317.     {
  318.         STRING lineBuf;
  319.         INTEGER len;
  320.  
  321.         if ((this.filesWithoutVal) && (this.filesWithoutVal.length() > 0)) {
  322.             len = this.filesWithoutVal.length();
  323.             INTEGER i;
  324.             lineBuf = "The following files were new \n\n";
  325.             lineBuf.Write(filename: filename, value: 1, mode: "a");
  326.             lineBuf.Write(value: 1);
  327.             for (i=0; i<len; i++) {
  328.                 lineBuf = "    "+this.filesWithoutVal.name(entry:i)+"\n";
  329.                 lineBuf.Write(filename: filename, value: 1, mode: "a");
  330.                 lineBuf.Write(value: 1);
  331.             }
  332.         } else {
  333.             lineBuf = "No new files found\n";
  334.             lineBuf.Write(filename: filename, value: 1, mode: "a");
  335.             lineBuf.Write(value: 1);
  336.         }
  337.         lineBuf = "\n\n";
  338.         lineBuf.Write(filename: filename, value: 1, mode: "a");
  339.         lineBuf.Write(value: 1);
  340.     }
  341.  
  342.     /****************************************************************************/
  343.     /************ private methods that:   Ingest input XML **********************/
  344.     /****************************************************************************/
  345.     private METHOD ingestDOM(XmlDocument vdoc)
  346.     {
  347.         XPath xpath = vdoc.createXPath();
  348.         XmlNodeSet nset = xpath.evaluate(expr: "/xipsValidation/filelist");
  349.         if ((!nset) || (nset.getLength() < 1)) {
  350.             this.isValid = FALSE;
  351.             return;
  352.         }
  353.         nset = xpath.evaluate(expr: "/xipsValidation/filelist/file");
  354.         if ((!nset) || (nset.getLength() < 1)) {
  355.             /* Its a real DOM but no files are given...okay just let it go */
  356.             this.isValid = TRUE;
  357.             return;
  358.         }
  359.         INTEGER cnt = nset.getLength();
  360.         INTEGER i;
  361.         XmlElement fiElem;
  362.         for(i=0; i<cnt; i++) {
  363.             fiElem = nset.item(index:i);
  364.             if (this.addFileData(fiElem: fiElem) == FALSE) {
  365.                 this.isValid = FALSE;
  366.                 return;
  367.             }
  368.         }
  369.         this.isValid = TRUE;
  370.     }
  371.  
  372.     private METHOD addFileData(XmlElement fiElem) RETURNS (BOOLEAN success)
  373.     {
  374.         STRING finm;
  375.         STRING fihs;
  376.         if (fiElem.hasAttribute(name: "name") == FALSE) {
  377.             success = FALSE;
  378.             return;
  379.         }
  380.         finm = fiElem.getAttribute(name: "name");
  381.         if (fiElem.hasAttribute(name: "hash") == FALSE) {
  382.             success = FALSE;
  383.             return;
  384.         }
  385.         fihs = fiElem.getAttribute(name: "hash");
  386.         this.nodeList.insert(name: finm, obj: fihs);
  387.         success = TRUE;
  388.     }
  389.  
  390.  
  391.  
  392.     /****************************************************************************/
  393.     /************ private methods that:   Build new XML *************************/
  394.     /****************************************************************************/
  395.     private METHOD getNewValidate ( ) RETURNS (XmlDocument vaData)
  396.     {
  397.         this.runProcess();
  398.         if (this.isValid == FALSE) return;
  399.  
  400.         XmlDocumentBuilder xdb;
  401.         XmlDocument rtv = xdb.newDocument();
  402.         XmlElement root = rtv.createElement(name: "xipsValidation");
  403.         rtv.setDocumentElement(node: root);
  404.         XmlElement filelist = rtv.createElement(name: "filelist");
  405.         root.appendChild(node:filelist);
  406.         /*this.addFilesFromList(doc: rtv, fielem: filelist, 
  407.                            fiList: this.validExistingFiles);
  408.         this.addFilesFromList(doc: rtv, fielem: filelist, 
  409.                            fiList: this.filesWithoutVal);
  410.         this.addFilesFromList(doc: rtv, fielem: filelist, 
  411.                            fiList: this.invalExistingFiles);
  412.         */
  413.         this.buildSortedList(vE: this.validExistingFiles, wV: this.filesWithoutVal,
  414.             iE: this.invalExistingFiles); 
  415.  
  416.         this.addFilesFromList(doc: rtv, fielem: filelist, 
  417.                            fiList: this.sortedFileList);
  418.         vaData = rtv;
  419.     }
  420.     private METHOD buildSortedList(LIST vE, LIST wV, LIST iE){
  421.     INTEGER i; 
  422.     for(i=0;i<vE.length();i++){
  423.            this.sortedFileList.insert(name: vE.name(entry: i), obj: vE.ref(entry: i));
  424.  //print vE.name(entry: i); print  vE.ref(entry: i);  
  425.     }
  426.         for(i=0;i<wV.length();i++){
  427.            this.sortedFileList.insert(name: wV.name(entry: i), obj: wV.ref(entry: i));
  428.         }
  429.         for(i=0;i<iE.length();i++){
  430.            this.sortedFileList.insert(name: iE.name(entry: i), obj: iE.ref(entry: i));
  431.         }
  432.  
  433.     this.sortedFileList.sort(lexical: 1, byname: 1);
  434.     }
  435.     private METHOD addFilesFromList(XmlDocument doc, XmlElement fielem, LIST fiList)
  436.     {
  437.         INTEGER len = fiList.length();
  438.         INTEGER i;
  439.         STRING fiName;
  440.         STRING fiHash;
  441.         XmlElement curElem;
  442.         for (i=0; i<len; i++) {
  443.             fiName = fiList.name(entry: i);
  444.             fiHash = fiList.ref(entry: i);
  445.             curElem = doc.createElement(name: "file");
  446.             curElem.setAttribute(value: fiName, name:"name");
  447.             curElem.setAttribute(value: fiHash, name:"hash");
  448.             fielem.appendChild(node: curElem);
  449.         }
  450.     }
  451.   
  452.     /*****************************************************************************
  453.     ********************    Internal Data Fields    ******************************
  454.     *****************************************************************************/
  455.     BOOLEAN isValid;
  456.     BOOLEAN isProcessed;
  457.     STRING fileDirectory;
  458.     STRING validFile;
  459.     LIST fileList;
  460.     LIST fileFilters; 
  461.     LIST nodeList;
  462.     LIST filesWithoutVal;
  463.     LIST invalExistingFiles;
  464.     LIST validExistingFiles;
  465.     LIST missingFiles;
  466.     LIST sortedFileList; 
  467. }
  468.  
  469.  
  470. /************************************************************************************
  471. **************************   SCRIPTING TO USE CLASS   *******************************
  472. ************************************************************************************/
  473. /* @vadof
  474. Validate a Directory of Files
  475.  
  476. // OVERVIEW
  477. This utility can be used to create a master validation list from a directory of files
  478. and later validate the same or similar directory of files for consistant checksums.
  479. The utility requires only one parameter - the path to the directory. Script behavior
  480. is dependent on the optional paramters.
  481.  
  482. // IMPORT PARAMETERS
  483. STRING directory is the directory that contains the set of files.
  484. STRING master    (optional) Is the name of a file (xml) that contains the validation
  485. list. If not provided, validation data will still be calculated for all files.
  486. STRING errorLog  (optional) If provided, this will be the name of a text file with
  487. a list of files that failed to match the master, a list of files that were
  488. missing from the directory based on the master list, and a list of new files.
  489. BOOLEAN mkTDmaster (optional) When TRUE, a new master will be created based on the
  490. existing directory of files. This may be done even when a master was given. The file
  491. will be naed with a timestamp and placed in the current working directory.
  492. STRING outputDir Prepend this path to the errorLog and the new validation master. Directory
  493. will be created if it does not exist.
  494. STRING flist Format "filters|ls|pdf,tif,...". When set the sciprt will only validate file with the given extensions. This is case insensitive.  
  495. */
  496.  
  497. IMPORT STRING directory;
  498. IMPORT STRING master;
  499. IMPORT STRING errorLog;
  500. IMPORT BOOLEAN mkTDmaster;
  501. IMPORT STRING outputDir;
  502. IMPORT STRING flist; 
  503.  
  504. // Can be exported
  505. BOOLEAN success = FALSE;
  506. LIST filterList ;  
  507. INTEGER newFiles = 0;
  508. INTEGER errorCnt = 0;
  509. ARGTOKEN akf; 
  510.  
  511.     if (!directory) {
  512.        print "vadof: name of directory is required use directory:s pathname";
  513.        SetStatus(op: "stop", msg: "vadof: name of directry is required");
  514.     } else {
  515.        if (outputDir) {
  516.           FILE od = new(FILE, path: outputDir);
  517.           if (od.exists() == FALSE) {
  518.              od.mkdirs();
  519.           } else {
  520.              if (od.isDirectory() == FALSE) {
  521.                 print "vadof: outputDir provided is not a directory";
  522.                 SetStatus(op: "stop", msg: "vadof: outputDir provided is not a directory");
  523.                 return;
  524.             }
  525.         }
  526.     }
  527.    
  528.     if(flist){
  529.  
  530.     akf= new(ARGTOKEN, param: flist); 
  531.     filterList = akf.getValue(); 
  532.         INTEGER i, len = filterList.length();
  533.     LIST ltemp;
  534.     STRING stemp, upper, lower;  
  535.     for(i=0;i<len;i++){
  536.       stemp = filterList.ref(entry: i);
  537.       upper = "."+stemp.toUpper(); 
  538.           lower = "."+stemp.toLower(); 
  539.           ltemp.insert(name: upper, obj: upper); 
  540.       ltemp.insert(name: lower, obj: lower);
  541.         }
  542.     filterList = ltemp;
  543.     } 
  544.   
  545.  
  546.     VADOF valObj = new(VADOF, directory: directory, validation: master, filters: filterList);
  547.     if (!valObj) return;
  548.  
  549.     valObj.diff ( ) Returns (success: success, errCnt: errorCnt, newCnt: newFiles);
  550.     if (success == FALSE) {
  551.         print "vadof result: errors = "+errorCnt+" new files = "+newFiles;
  552.         SetStatus(op: "stop", msg: "VADOF: processing was unsuccessful");
  553.         return;
  554.     }
  555.  
  556.     if (mkTDmaster == TRUE) {
  557.         STRING uniqFile;
  558.         DATETIME tm;
  559.         tm.setToCurrent(utc: FALSE);
  560.         uniqFile = "validate"+tm.toString()+".xml";
  561.         if (outputDir) {
  562.             uniqFile = outputDir+FILE.sep()+uniqFile;
  563.         }
  564.         success = valObj.updateValidateFile(filename: uniqFile);
  565.         if (success == FALSE) return;
  566.     }
  567.  
  568.     if (errorLog) {
  569.         if (outputDir) {
  570.             errorLog = outputDir+FILE.sep()+errorLog;
  571.         }
  572.         success = valObj.logErrors(filename: errorLog);
  573.     }
  574.  
  575.     print "vadof result: errors = "+errorCnt+" new files = "+newFiles;
  576.     if ((errorCnt != 0) || (newFiles != 0)) {
  577.         SetStatus(op: "stop", msg: "vadof: resulted in errors or new files");
  578.     }
  579. }
  580.  
  581.  
  582.